import 'dart:async';

import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:zrjz/globalService/user_service.dart';
import 'package:zrjz/modules/auth/login/login_page.dart';
import 'package:zrjz/router/page_router.dart';

/*
  登录拦截与转发
 */
class LoginIntercepManager {
  static LoginIntercepManager? _threadManager;
  static Completer<bool>? _completer;

  LoginIntercepManager._();

  static LoginIntercepManager get() {
    _threadManager ??= LoginIntercepManager._();
    return _threadManager!;
  }

  void checkLogin(void Function() nextRunnable) {
    if (UserService.to.isLogin) {
      // 已经登录
      nextRunnable();
      return;
    }

    // 如果没有登录-先去登录页面
    LoginPage.startInstance();

    // 等待登录完成
    _completer = Completer<bool>();
    _completer?.future.then((result) {
      if (UserService.to.isLogin) {
        // 已经登录
        nextRunnable();

        _completer = null;
      } else {

        SmartDialog.showToast('还未登录成功，请登录之后再试');
      }
    });
  }

  // 设置登录完成
  void loginFinished() {
    _completer?.complete(true);
  }
}
